package cn.zl.biz.controller.router;

import cn.zl.biz.entity.SysUser;
import cn.zl.biz.service.ArticleService;
import cn.zl.biz.service.CommentService;
import cn.zl.biz.service.TagService;
import cn.zl.biz.service.UserService;
import cn.zl.common.controller.BaseController;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 博客后台页面路由
 *
 * @author zl
 * @date 2023/9/12
 */
@Controller
public class TumoRouterController extends BaseController {

    @Resource
    private ArticleService articleService;

    @Resource
    private CommentService commentService;

    @Resource
    private TagService tagService;

    @Resource
    private UserService userService;

    /**
     * 注销接口
     *
     */
    @GetMapping(value = "/logout")
    public String logout() {
        Subject subject = getSubject();
        subject.logout();
        return "redirect:/login";
    }

    /**
     * 登录状态校验
     * Model是一个用于传递数据给视图的重要工具
     */
    private boolean auth(HttpServletRequest request, Model model) {
        Object o = request.getSession().getAttribute("user");
        model.addAttribute("user", o);
        return o instanceof SysUser;
    }

    /**
     * 除Session中的用户信息后返回登录页面视图
     * @param request
     */
    @GetMapping("/login")
    public String login(HttpServletRequest request) {
        request.getSession().removeAttribute("user");
        return "login";
    }

    /**
     * 清除Session中的用户信息后返回注册页面视图
     * @return
     */
    @GetMapping("/register")
    public String register(HttpServletRequest request) {
        request.getSession().removeAttribute("user");
        return "register";
    }

    /**
     * 判断是否登录
     * @param model
     */
    @GetMapping("/zl")
    public String index(HttpServletRequest request, Model model) {
        if (!this.auth(request, model)) {
            return "redirect:/login";
        }
        //添加文章数量标签数量等到model中
        model.addAttribute("articleCount", articleService.count());
        model.addAttribute("tagCount", tagService.count());
        model.addAttribute("commentCount", commentService.count());
        model.addAttribute("userCount", userService.count());
        return "zl/index/index";
    }

    /**
     * 显示个人资料页面
     * @param request
     * @param model
     */
    @GetMapping("/zl/profile")
    public String profile(HttpServletRequest request, Model model) {
        if (!this.auth(request, model)) {
            return "redirect:/login";
        }
        return "zl/profile/index";
    }

    /**
     * 显示文章编辑页面
     * @param request
     * @param model
     * @return
     */
    @GetMapping("/zl/article/write")
    public String articleWrite(HttpServletRequest request, Model model) {
        if (!this.auth(request, model)) {
            return "redirect:/login";
        }
        return "zl/article/write/index";
    }

    /**
     * 显示文章列表页面
     * @param request
     * @param model
     * @return
     */
    @GetMapping("/zl/article/list")
    public String articleList(HttpServletRequest request, Model model) {
        if (!this.auth(request, model)) {
            return "redirect:/login";
        }
        return "zl/article/list/index";
    }

    /**
     * 标签管理页
     * @param request
     * @param model
     * @return
     */
    @GetMapping("/zl/blog/tag")
    public String blogTag(HttpServletRequest request, Model model) {
        if (!this.auth(request, model)) {
            return "redirect:/login";
        }
        return "zl/blog/tag/index";
    }

    /**
     * 显示分类管理页面
     * @param request
     * @param model
     * @return
     */
    @GetMapping("/zl/blog/category")
    public String blogCategory(HttpServletRequest request, Model model) {
        if (!this.auth(request, model)) {
            return "redirect:/login";
        }
        return "zl/blog/category/index";
    }

    /**
     * 友情链接页面
     * @param request
     * @param model
     * @return
     */
    @GetMapping("/zl/blog/link")
    public String blogLink(HttpServletRequest request, Model model) {
        if (!this.auth(request, model)) {
            return "redirect:/login";
        }
        return "zl/blog/link/index";
    }

    /**
     * 评论管理页面
     * @param request
     * @param model
     * @return
     */
    @GetMapping("/zl/blog/comment")
    public String blogComment(HttpServletRequest request, Model model) {
        if (!this.auth(request, model)) {
            return "redirect:/login";
        }
        return "zl/blog/comment/index";
    }

    /**
     * 系统日志管理页面
     * @param request
     * @param model
     * @return
     */
    @GetMapping("/zl/setting/log")
    public String settingLog(HttpServletRequest request, Model model) {
        if (!this.auth(request, model)) {
            return "redirect:/login";
        }
        return "zl/setting/log/index";
    }


}
